-- game_server.lua

local skynet = require "skynet"

local M = {}

local room_list = {} -- 存储房间列表

-- 创建房间
function M.create_room(room_id)
    if room_list[room_id] then
        skynet.error(string.format("Room %d already exists!", room_id))
        return nil
    end

    local room = {
        id = room_id,
        player_list = {}, -- 存储玩家列表
        frame_count = 0, -- 当前帧数
        max_player = 2, -- 最大玩家数
        max_frame = 100, -- 最大帧数
        frame_data = {}, -- 存储每帧的数据
    }

    room_list[room_id] = room

    skynet.error(string.format("Create Room %d!", room_id))

    return room
end

-- 加入房间
function M.join_room(room_id, player_id)
    local room = room_list[room_id]
    if not room then
        skynet.error(string.format("Room %d does not exist!", room_id))
        return nil
    end

    if #room.player_list >= room.max_player then
        skynet.error(string.format("Room %d is full!", room_id))
        return nil
    end

    local player = {
        id = player_id,
        room_id = room_id,
        last_frame = 0, -- 玩家最后一帧的帧号
        frame_data = {}, -- 玩家发送的数据
    }

    table.insert(room.player_list, player)

    skynet.error(string.format("Player %d joins Room %d!", player_id, room_id))

    return player
end

-- 开始帧同步
function M.start_frame_sync(room_id)
    local room = room_list[room_id]
    if not room then
        skynet.error(string.format("Room %d does not exist!", room_id))
        return
    end

    if #room.player_list < room.max_player then
        skynet.error(string.format("Room %d is not full yet!", room_id))
        return
    end

    for i, player in ipairs(room.player_list) do
        player.last_frame = 0
    end

    while room.frame_count <= room.max_frame do
        -- 检查每个玩家的帧数据是否都已到齐
        local all_data_received = true
        for i, player in ipairs(room.player_list) do
            if not player.frame_data[room.frame_count] then
                all_data_received = false
                break
            end
        end

        -- 如果所有玩家的帧数据都到齐了，就开始处理这一帧的数据
        if all_data_received then
            local frame_data = {}
            for i, player in ipairs(room.player_list) do
                local player_data = player.frame_data[room.frame_count]
                table.insert(frame_data, player_data)
                player.last_frame = room.frame_count
            end

            -- 处理这一帧的数据
            skynet.send(room.game_server, "lua", "handle_frame_data", room_id, room.frame_count, frame_data)

            room.frame_count = room.frame_count + 1
        end

        skynet.sleep(1) -- 等待 1
        -- 处理这一帧的数据
        skynet.send("room_" .. room_id, "lua", "process_frame_data", frame_data)

        -- 广播帧数据给所有玩家
        for i, player in ipairs(room.player_list) do
            skynet.send("room_" .. room_id, "lua", "broadcast_frame_data", player.id, frame_data)
        end

        -- 当前帧数+1
        room.frame_count = room.frame_count + 1

        -- 清空玩家帧数据
        for i, player in ipairs(room.player_list) do
            player.frame_data[room.frame_count] = nil
        end

        -- 如果达到最大帧数，就结束帧同步
        if room.frame_count > room.max_frame then
            skynet.error(string.format("Frame sync finished in Room %d!", room_id))
            skynet.send("room_" .. room_id, "lua", "frame_sync_finished")
            room_list[room_id] = nil
        end

        -- 休眠一定时间，等待下一帧数据的到来
        skynet.sleep(100) -- 100毫秒，根据具体情况调整
    end
end

return M
